package MyQueue;

public class MyCycleQueue {
	private int [] elem;
	private int front;//队头下标
	private int rear;//队尾下标
	//这里使用的方法是空出来一个位置，来进行判满
	public MyCycleQueue(int k) {
		this.elem = new int [k+1];
	}
	//入队
	public boolean enQueue (int value) {
		if(isFull() ) {
			return false;
		}
		this.elem[rear]=value;
		rear=(rear+1)%this.elem.length;
		return true;
	}
	public boolean deQueue() {
		//盘满过后如何进行扩容
		//周日的任务。思考一下
		if(isFull()) {
			return false;
		}
		front=(front+1)%this.elem.length;
		return true;
	}
	//获得队头元素
	public int Front() {
		if(isEmpty()) {
			return -1;
		}
		return this.elem[front];
	}
	//获得队尾元素
	public int Rear() {
		if(isEmpty()) {
			return -1;
		}
		int index=(rear==0)?this.elem.length-1:rear-1;
		return this.elem[index];
	}
	public boolean isEmpty() {
		return rear==front;
	}
	public boolean isFull() {
		return (rear+1)%this.elem.length==front;
	}
}
